package com.ciaj.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciaj.base.comm.constant.DefaultConstant;
import com.ciaj.base.comm.utils.CollectionUtil;
import com.ciaj.module.sys.entity.po.SysJobRoleRel;
import com.ciaj.module.sys.entity.po.SysRole;
import com.ciaj.module.sys.mapper.SysRoleMapper;
import com.ciaj.module.sys.service.ISysJobRoleRelService;
import com.ciaj.module.sys.service.ISysRoleService;
import com.ciaj.base.bean.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* <p>
* 角色表 Gen 服务实现类
* </p>
*
* @author ciaj.
* @since 2020-08-14
*/
@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private ISysJobRoleRelService iSysJobRoleRelService;

    @Override
    public Set<String> getRoleCodesByJobId(String jobId) {
        Set<String> roleCodes = CollectionUtil.of(getRolesByJobId(jobId)).map(SysRole::getCode).collect(Collectors.toSet());
        return roleCodes;
    }

    @Override
    public List<SysRole> getRolesByJobId(String jobId) {
        List<SysJobRoleRel> jobRoleRels = iSysJobRoleRelService.list(new QueryWrapper<SysJobRoleRel>().lambda().eq(SysJobRoleRel::getJobId, jobId));

        Set<String> roleIds = CollectionUtil.of(jobRoleRels).map(SysJobRoleRel::getRoleId).collect(Collectors.toSet());
        List<SysRole> roles = super.list(new QueryWrapper<SysRole>().lambda().eq(SysRole::getAvailable, DefaultConstant.FLAG_Y).in(SysRole::getId, roleIds));

        return roles;
    }
}
